<?php
/**
 * Created by PhpStorm.
 * User: mayn
 * Date: 2018/6/13
 * Time: 15:13
 */

namespace App\Modules\Manage\Model;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class InformationModel extends Model
{
    protected $table = 'information';
    protected $primaryKey ='id';

    protected $fillable=[
      'title', 'content', 'key','type', 'is_show', 'sort', 'pic'
    ];

    /**
     * @param string $type
     * @param $is_object boolean 是否需要返回对象
     * @return array| object
     */
    static function getList($is_object, $type= '')
    {

        if($type != '')
        {
            $list = self::selet('*')->where('type', $type)->get();
        }
        else
        {
            $list = self::select('*')->get();
        }
        if($is_object)
        {
            return $list;
        }
        return !empty($list) ? $list->toArray(): [];
    }

    static function getInfoByType($type, $paginate =6)
    {
        if(Cache::has('info_'.$type))
        {
            $aboutUs = Cache::get('info_'.$type);
        }
        else
        {
            $aboutUs = self ::where('type', $type)->orderBy('sort', 'ASC')->paginate($paginate);
            Cache::put('info_'.$type, $aboutUs, 60*24);
        }
        return $aboutUs;

    }

    static function getInfById($id)
    {
        $info = self::where('id', $id)->first();
        if(!empty($info))
        {
            return $info->toArray();
        }
       return [];
    }
}